﻿#region Copyright (c) 11/27/2011, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using NUnit.Framework;


namespace Amarok.Presentation
{
	[TestFixture]
	public class Test_ViewActionHandlerInfo
	{
		private class MyViewAction : ViewAction
		{
		}

		private class MyClass
		{
			public static MyViewAction Argument;
			public static Int32 Called;

			public Boolean Method1(MyViewAction argument)
			{
				Argument = argument;
				Called++;
				return true;
			}

			public static Boolean Method2(MyViewAction argument)
			{
				Argument = argument;
				Called++;
				return true;
			}
		}


		// --- TESTS ---


		[Test]
		public void Test_CreateHandler_InvokeHandler()
		{
			var target = new MyClass();
			var methodInfo = target.GetType().GetMethod("Method1");
			var parameterInfo = methodInfo.GetParameters()[0];

			var handlerInfo = new ViewActionHandlerInfo(methodInfo, parameterInfo);

			Assert.That(handlerInfo.MethodInfo, Is.EqualTo(methodInfo));
			Assert.That(handlerInfo.ParameterInfo, Is.EqualTo(parameterInfo));
			Assert.That(handlerInfo.ViewActionType, Is.EqualTo(typeof(MyViewAction)));

			MyClass.Argument = null;
			MyClass.Called = 0;

			var handler = handlerInfo.CreateHandler(target);

			Assert.That(MyClass.Called, Is.EqualTo(0));
			Assert.That(MyClass.Argument, Is.Null);

			var message1 = new MyViewAction();
			var result1 = handler.Invoke(message1);

			Assert.That(MyClass.Called, Is.EqualTo(1));
			Assert.That(MyClass.Argument, Is.SameAs(message1));
			Assert.That(result1, Is.True);

			var message2 = new MyViewAction();
			var result2 = handler.Invoke(message2);

			Assert.That(MyClass.Called, Is.EqualTo(2));
			Assert.That(MyClass.Argument, Is.SameAs(message2));
			Assert.That(result2, Is.True);
		}

		[Test]
		public void Test_CreateHandler_InvokeHandler_StaticMethod()
		{
			var target = new MyClass();
			var methodInfo = target.GetType().GetMethod("Method2");
			var parameterInfo = methodInfo.GetParameters()[0];

			var handlerInfo = new ViewActionHandlerInfo(methodInfo, parameterInfo);

			Assert.That(handlerInfo.MethodInfo, Is.EqualTo(methodInfo));
			Assert.That(handlerInfo.ParameterInfo, Is.EqualTo(parameterInfo));
			Assert.That(handlerInfo.ViewActionType, Is.EqualTo(typeof(MyViewAction)));

			MyClass.Argument = null;
			MyClass.Called = 0;

			var handler = handlerInfo.CreateHandler(null);

			Assert.That(MyClass.Called, Is.EqualTo(0));
			Assert.That(MyClass.Argument, Is.Null);

			var message = new MyViewAction();
			var result = handler.Invoke(message);

			Assert.That(MyClass.Called, Is.EqualTo(1));
			Assert.That(MyClass.Argument, Is.SameAs(message));
			Assert.That(result, Is.True);
		}

		[Test]
		public void Test_Construction_NullMethodInfo()
		{
			var target = new MyClass();
			var methodInfo = target.GetType().GetMethod("Method1");
			var parameterInfo = methodInfo.GetParameters()[0];

			Assert.Throws<ArgumentNullException>(() =>
				{
					var handlerInfo = new ViewActionHandlerInfo(null, parameterInfo);
				});
		}

		[Test]
		public void Test_Construction_NullParameterInfo()
		{
			var target = new MyClass();
			var methodInfo = target.GetType().GetMethod("Method1");

			Assert.Throws<ArgumentNullException>(() =>
				{
					var handlerInfo = new ViewActionHandlerInfo(methodInfo, null);
				});
		}

	}
}

#endif
